<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="用户名称" prop="createBy">
        <el-input
          v-model="queryParams.createBy"
          placeholder="请输入姓名"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
          :disabled = "createVisible"
        />
      </el-form-item>
      <el-form-item label="手机号码" prop="tel">
        <el-input
          v-model="queryParams.tel"
          placeholder="请输入手机号码"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="预约时间" prop="bookTime">
        <el-date-picker clearable size="small"
          v-model="queryParams.bookTime"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="选择预约时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="姓名" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入姓名"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['business:book:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['business:book:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="bookList" @selection-change="handleSelectionChange">
      <el-table-column label="系统用户名" align="center" prop="createBy" />
      <el-table-column label="手机号码" align="center" prop="tel" />
      <el-table-column label="预约时间" align="center" prop="bookTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.bookTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="名称" align="center" prop="name" />
      <el-table-column label="描述备注" align="center" prop="describe" />
      <el-table-column label="预约状态" align="center" prop="status" :formatter="statusFormat" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <!-- <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['business:book:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['business:book:remove']"
          >删除</el-button> -->
          <el-button
            size="mini"
            type="text"
            icon="el-icon-circle-close"
            @click="handleCancel(scope.row)"
            :disabled="scope.row.status != 1 "
            v-hasPermi="['business:book:cancel']"
          >取消</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-circle-check"
            @click="handleConfirm(scope.row)"
            v-hasPermi="['business:book:confirm']"
            :disabled="scope.row.status != 1 "
          >已到达</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改预约对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="手机号码" prop="tel">
          <el-input v-model="form.tel" placeholder="请输入手机号码" />
        </el-form-item>
        <el-form-item label="预约时间" prop="bookTime">
          <el-date-picker clearable size="small"
            v-model="form.bookTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="选择预约时间"
            @change="selectDate"
          :picker-options="pickerOptions">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="剩余预约个数">
          <span>{{this.num}}</span>
        </el-form-item>
        <el-form-item label="描述备注" prop="describe">
          <el-input v-model="form.describe" placeholder="请输入备注" />
        </el-form-item>
        <el-form-item label="姓名" prop="name">
          <el-input v-model="form.name" placeholder="请输入姓名" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm" :disabled="buttonDisable">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listBook, getBook, delBook, addBook, updateBook, exportBook ,cancelBook,confirmBook,bookNum} from "@/api/business/book";
import { getRole } from "@/api/business/book";
export default {
  name: "Book",
  components: {
  },
  data() {
    return {
      //提交按钮可用状态
      buttonDisable:false,
      //禁用掉当前系统时间之前的时间
      pickerOptions: {
         disabledDate(time) {
            return time.getTime() < Date.now() ;
          },
        },  
      //
      createVisible:false,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 预约表格数据
      bookList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 预约状态字典
      statusOptions: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        createBy: undefined,
        tel: null,
        bookTime: null,
        name: null
      },
      //剩余个数
      num:5,
      // 表单参数
      form: {},
      //按日期查询参数
      time:{
        bookTime:undefined,
      },
      // 表单校验
      rules: {
      }
    };
  },
  created() {
   this.getDept();
    this.getDicts("poppy_book_status").then(response => {
      this.statusOptions = response.data;
    });
  },
  methods: {
    //获取当日是否有预约名额
    selectDate(val){
      this.num = 5;
      this.time.bookTime = val;
      bookNum(this.time).then(response=>{
        this.num = this.num-response;
        if(this.num == 0){
          this.buttonDisable = true;
          this.msgError(val+"已无预约名额");
        }else{
          this.buttonDisable = false;
        }
      })
    },
    //获取当前登陆用户所属部门
    getDept(){
      getRole().then(response=>{
        console.log(response)
        if(response.deptId == 205){
          this.queryParams.createBy = response.userName;
          this.createVisible = true;
        }
        
        this.getList();
      })
    },
    /** 查询预约列表 */
    getList() {
      this.loading = true;
      listBook(this.queryParams).then(response => {
        this.bookList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 预约状态字典翻译
    statusFormat(row, column) {
      return this.selectDictLabel(this.statusOptions, row.status);
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        createBy: null,
        createTime: null,
        tel: null,
        bookTime: null,
        describe: null,
        status: "0",
        name: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加预约";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getBook(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改预约";
      });
    },
    /** 提交按钮 */
    submitForm() {
      console.log(this.form);
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateBook(this.form).then(response => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addBook(this.form).then(response => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm('是否确认删除预约编号为"' + ids + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delBook(ids);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        })
    },
    /** 取消按钮操作 */
    handleCancel(row){
      this.$confirm('是否确认取消"' + row.bookTime + '"的预约?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return cancelBook(row.id);
        }).then(() => {
          this.getList();
          this.msgSuccess("取消预约成功");
        })
    },
    /** 已到达按钮操作 */
    handleConfirm(row){
      this.$confirm('是否确认系统用户名为"' + row.createBy + '"的顾客已到达?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return confirmBook(row.id);
        }).then(() => {
          this.getList();
          this.msgSuccess("已将预约状态改为已到达");
        })
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有预约数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return exportBook(queryParams);
        }).then(response => {
          this.download(response.msg);
        })
    }
  }
};
</script>
